<!--
描述：直播课审核
开发人：zhaohang
开发日期：2019年11月15日
-->
<template lang="pug">
  kalix-dialog.user-add(title='审核' bizKey="liveExamine" ref="kalixBizDialog" v-bind:formModel.sync="formModel" v-bind:targetURL="targetURL" :submitActionReactor="submitActionReactor")
    div.el-form(slot="dialogFormSlot")
      el-form-item(label="课程名称" prop="className")
        el-input(v-model="formModel.className" readonly="readonly")
      el-form-item.short(label="课程封面" prop="classCover")
        img(:src="formModel.classCover" height="100px" readonly="readonly")
      el-form-item(label="教师名称" prop="teacher")
        el-input(v-model="formModel.teacher" readonly="readonly")
      el-form-item(label="课程简介" prop="classIntroduce")
        el-input(v-model="formModel.classIntroduce" readonly="readonly" type="textarea" v-bind:rows="3")
      el-form-item(label="教师简介" prop="teacherIntroduce")
        el-input(v-model="formModel.teacherIntroduce" readonly="readonly" type="textarea" v-bind:rows="3")
      el-form-item(label="开课时间" prop="openTime")
        <!--el-input(v-model="formModel.openTime" readonly="readonly")-->
        kalix-datetime-picker(v-model="formModel.openTime" type="date" readonly)
      <!--el-form-item(label="是否评价" prop="appraise")-->
      <!--el-input(v-model="formModel.appraise" readonly="readonly")-->
      el-form-item(label="课件")
        div(v-for="item in arr")
          p.download(@click="download(item)") {{item}}
      el-form-item(label="是否推荐" prop="recommend" v-bind:rules="rules.recommend")
        el-select(v-model="formModel.recommend"  placeholder="请选择")
          el-option(v-for="item in optionRecommend" v-bind:key="item.value" v-bind:value="item.value" v-bind:label="item.label")
      el-form-item(label="审核" prop="temp" v-bind:rules="rules.temp")
        el-select(v-model="formModel.temp"  placeholder="请选择")
          el-option(v-for="item in option" v-bind:key="item.value" v-bind:value="item.value" v-bind:label="item.label")
      el-form-item(label="原因" prop="reason" v-if="formModel.temp===2" v-bind:rules="rules.reason")
        el-input(v-model="formModel.reason" type="textarea")
</template>

<script type="text/ecmascript-6">
  import {liveURL, classNoticeURL} from '../../config.toml'
  import FormModel from './model'
  import Message from '../../../../../common/message'
  import EventBus from '../../../../../components/corelib/common/eventbus'
  import {ON_REFRESH_DATA} from '../../../../../components/corelib/components/common/event.toml'

  export default {
    name: 'liveExamine',
    data() {
      return {
        arr: [],
        fileList: [],
        formModel: Object.assign({}, FormModel),
        option: [{
          value: 1,
          label: '通过'
        }, {
          value: 2,
          label: '不通过'
        }
        ],
        optionRecommend: [{
          value: 1,
          label: '是'
        }, {
          value: 2,
          label: '否'
        }
        ],
        rules: {
          temp: [{required: true, message: '请审核', trigger: 'change'}],
          reason: [{required: true, message: '请填写原因', trigger: 'blur'}],
          recommend: [{required: true, message: '请选择', trigger: 'blur'}]
        },
        labelWidth: '120px',
        targetURL: liveURL
      }
    },
    mounted() {
      setTimeout(() => {
        this.arr = this.formModel.courseware.split(',')
      }, 500)
    },
    methods: {
      download(fileName) {
        let urls = fileName
        // window.location.href = urls
        window.open(urls)
      },
      submitActionReactor(baseDialog) {
        this.arr = this.formModel.courseware.split(',')
        // if (baseDialog.formModel.appraise === '否') {
        //   this.formModel.appraise = 'false'
        // } else {
        //   this.formModel.appraise = 'true'
        // }
        console.log('11111111111111111111111111111', baseDialog.formModel)
        if (baseDialog.formModel.recommend === '是') {
          this.formModel.recommend = 'true'
        }
        if (baseDialog.formModel.recommend === '否') {
          this.formModel.recommend = 'false'
        }
        if (baseDialog.formModel.temp === '通过') {
          this.formModel.temp = 1
        }
        if (baseDialog.formModel.temp === '不通过') {
          this.formModel.temp = 2
        }
        if (baseDialog.formModel.isOpen === '待审核') {
          this.formModel.isOpen = 0
        } else if (baseDialog.formModel.isOpen === '是') {
          this.formModel.isOpen = 1
        } else if (baseDialog.formModel.isOpen === '否') {
          this.formModel.isOpen = 2
        }
        this.formModel.isExamine = this.formModel.temp
        if (baseDialog.formModel.isExamine === 1) {
          this.formModel.notice = '您申请的《' + this.formModel.className + '》课已通过审核！'
        } else {
          this.formModel.notice = '您申请的《' + this.formModel.className + '》课未通过审核，详情进入课程管理查看！'
          this.formModel.isOpen = 2
        }
        if (baseDialog.formModel.overTime !== null) {
          this.axios.request({
            method: 'GET',
            url: liveURL + '/' + this.formModel.id,
            data: {
              id: this.formModel.id
            }
          }).then(res => {
            this.axios.request({
              method: 'PUT',
              url: liveURL + '/' + this.formModel.id,
              data: {
                id: this.formModel.id,
                className: this.formModel.className,
                classIntroduce: this.formModel.classIntroduce,
                teacherIntroduce: this.formModel.teacherIntroduce,
                classCover: this.formModel.classCover,
                isOpen: this.formModel.isOpen,
                isExamine: this.formModel.isExamine,
                appraise: this.formModel.appraise,
                openTime: this.formModel.openTime,
                browseNum: this.formModel.browseNum,
                teacherId: this.formModel.teacherId,
                personNum: this.formModel.personNum,
                nowNum: res.data.nowNum,
                overTime: this.formModel.overTime,
                courseware: this.formModel.courseware,
                reason: this.formModel.reason,
                recommend: this.formModel.recommend
              }
            }).then(response => {
              this.axios.request({
                method: 'POST',
                url: classNoticeURL,
                data: {
                  notice: this.formModel.notice,
                  personName: this.formModel.teacher,
                  new: true,
                  personId: this.formModel.teacherId
                }
              }).then(response => {
                if (response.data.success) {
                  Message.success(response.data.msg)
                  EventBus.$emit('liveExamine' + '-' + 'KalixDialogClose')
                  // this.$refs.dialogForm.resetFields()
                  // this.submitComplete()
                } else {
                  Message.error(response.data.msg)
                  EventBus.$emit('liveExamine' + '-' + 'KalixDialogClose')
                }
                // }
                // 刷新列表
                console.log(' ===== BaseDialog ON_REFRESH_DATA ===== ', ON_REFRESH_DATA)
                EventBus.$emit(ON_REFRESH_DATA, 'moocClassExamine')
                console.log('[kalix] dialog submit button clicked !')
              })
            })
          })
        } else {
          Message.error('请先选择报名截止时间！')
          EventBus.$emit('liveExamine' + '-' + 'KalixDialogClose')
          console.log(' ===== BaseDialog ON_REFRESH_DATA ===== ', ON_REFRESH_DATA)
          EventBus.$emit(ON_REFRESH_DATA, 'liveOpen')
          console.log('[kalix] dialog submit button clicked !')
        }
      }
    }
  }
</script>

<style scoped lang="stylus" type="text/stylus">
  .download
    cursor pointer
    text-decoration underline
    color #2d8ac7
    display inline-block
</style>
